package fr.univ.context.data.info;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

public class InfoRunningApplications {
	
	private Context context;
	
	public InfoRunningApplications(Context context){
		this.context = context;
	}
	
	// check all running applications
	public Hashtable<String, String> getListNameAppRunning(){
		
		ActivityManager actvityManager = (ActivityManager) getContext().getSystemService( Context.ACTIVITY_SERVICE );
		List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
		PackageManager pm = context.getPackageManager();
		
		Iterator<RunningAppProcessInfo> i = procInfos.iterator();
		
		Hashtable<String, String> process = new Hashtable<String, String>();
		
		while(i.hasNext()){
			RunningAppProcessInfo p = i.next();
			try {
				CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(p.processName,PackageManager.GET_META_DATA));
				process.put(c.toString(),p.processName);
			} catch (NameNotFoundException e) {
				e.printStackTrace();
			}
		}	
		return process;
	}

	private Context getContext() {
		return context;
	}
}
